import Immutable from 'immutable';
import { queryHasBuyMenber, invoiceEscort } from '../services/escort';
// import toast from '../utils/toast';

export default {
  namespace: 'escort',
  state: Immutable.fromJS({
    hasBuyMenber: false,
    menberActive: false,
    escortBuyMonth: 3,
  }),

  effects: {
    *fetchInvoiceEscort({ payload }, { call }) {
      const res = yield call(invoiceEscort, payload);
      if (res.success) {
        window.location.href = res.data.url;
      }
    },
    *fetchBuyStatus({ payload }, { call, put }) {
      const res = yield call(queryHasBuyMenber, payload);
      if (res.success) {
        yield put({
          type: 'updateBuyStatus',
          payload: res.data,
        });
      }
    },
  },

  reducers: {
    updateBuyStatus(state, { payload }) {
      return state.merge({
        hasBuyMenber: payload.escortBuyResult === 1,
        menberActive: payload.lightenUp === 1,
        escortBuyMonth: payload.escortBuyMonth,
      });
    },
  },
};
